<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Cart extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function Cart() {
		parent::__construct();
		$this->load->model('helpermodel');
	}

	public function index() {
		$data['module'] = "cart";
		$data['view_file'] = "cart";

		$this->load->model('listingmap/mdl_listingmapmodel');
		$options = array();

		//refer to Int listing search
		$country = $this->input->get('country');
		if (isset($country) && !empty($country)) {
			$options['country'] = $country;
		} else {
			$options['country'] = 8;
		}

		$options['cities'] = NULL;

		$locations = $this->input->get('district');
		$loc_value = array();

		if ($this->helpermodel->check_is_array($locations)) {
			foreach ($locations as $key => $value) {
				if (!empty($value) && !is_null($value)) {
					if (strpos($value, '->') !== false) {
						list($key, $val) = explode('->', $value);
						array_push($loc_value, $val);
					}
				}
			}
		}
		if ($this->helpermodel->check_is_array($loc_value)) {
			$options['locations'] = implode(',', $loc_value);
		} else {
			$options['locations'] = NULL;
		}

		$subarea = $this->input->get('chkSubArea');

		if ($this->helpermodel->check_is_array($subarea)) {
			$options['subarea'] = implode(',', $this->input->get('chkSubArea'));
		} else {
			$options['subarea'] = NULL;
		}

		$options['list_type'] = NULL;
		$options['propertyfor'] = NULL;
		$options['location'] = NULL;
		$options['propertytype'] = NULL;
		$options['price_min'] = NULL;
		$options['price_max'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['bedrooms'] = NULL;
		$options['bathrooms'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "ListingDate DESC";

		//Features
		$features = $this->input->get('feature_category');

		if ($this->helpermodel->check_is_array($features)) {
			$options['features'] = implode(',', $features);
		} else {
			$options['features'] = NULL;
		}

		//Facilities
		$facilities = $this->input->get('facilities_category');

		if ($this->helpermodel->check_is_array($facilities)) {
			$options['facilities'] = implode(',', $facilities);
		} else {
			$options['facilities'] = NULL;
		}

		$results = $this->mdl_listingmapmodel->getListingWithWhere($options);

		$data['listings'] = $results;

		echo Modules::run('template/default_template', $data);
	}

	function add_cart_item() {
		$this->load->model('cart_model');

		$options['country'] = NULL;
		$options['cities'] = NULL;
		$options['list_type'] = NULL;
		$options['propertyfor'] = NULL;
		$options['location'] = NULL;
		$options['subarea'] = NULL;
		$options['propertytype'] = NULL;
		$options['price_min'] = NULL;
		$options['price_max'] = NULL;
		$options['size_min'] = NULL;
		$options['size_max'] = NULL;
		$options['bedrooms'] = NULL;
		$options['bathrooms'] = NULL;
		$options['keyword'] = NULL;
		$options['orderby'] = "ListingDate DESC";
		$options['features'] = NULL;
		$options['facilities'] = NULL;
		$limit['offset'] = 0;
		$limit['limit'] = 999999;
		$options['language'] = 1;
		$id = $this->input->post('listingId');

		if ($this->cart_model->validate_add_cart_item($options, $id) == TRUE) {
			//check if user has javascript enabled
			if ($this->input->post('ajax') != '1') {
				redirect('cart');
			} else {
				echo 'true';
			}
		}
	}

	function update_cart() {
		$this->cart_model->validate_update_cart();
		redirect('cart');
	}

	function empty_cart() {
		$this->cart->destroy(); // Destroy all cart data
		redirect('cart'); // Refresh te page
	}

	function checkout() {
		$data['module'] = "cart";
		$data['view_file'] = "checkout";

		echo Modules::run('template/default_template', $data);
	}

	function show_cart() {
		$data['module'] = "cart";
		$data['view_file'] = "checkout";

		echo Modules::run('template/default_template', $data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */